package com.leo.biz.service.pub;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by leo on 2016/7/30.
 */
public  class CommUtil {
    public static int getTodayCldate()
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        int ret=Integer.valueOf(df.format(new Date()).toString()).intValue();
        return ret;
    }

    public static long difToCurDate(String datestr)
    {
        long interval=-9999;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date temp=df.parse(datestr);
            Date curdate=new Date();
             interval = (temp.getTime() - curdate.getTime())/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  interval;
    }

    public static String getCurDate()
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
        String ret=df.format(new Date()).toString();
        return ret;
    }

    /*将List转换为以Key的Map*/
    public static  <T> Map<String,T> convertToHashMap(List<T> list,String key)
    {




        String getterName="get"+ Character.toUpperCase(key.charAt(0))+key.substring(1);
        Map<String,T> ret=new HashMap<>();
        for(int i=0;i<list.size();i++)
        {
            try {
                Method m = list.get(i).getClass().getDeclaredMethod(getterName);
                String mapKey=m.invoke(list.get(i)).toString();
                ret.put(mapKey,list.get(i));
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        }
        return ret;
    }

    public static  void log(String str){
        System.out.println("["+getCurDate()+"]:"+str);

    };
}
